package com.curluin.inputInterpretation;

import android.view.MotionEvent;

import com.curluin.view.Camera;
import com.curluin.controll.Controller;
import com.curluin.math.MyMatrix;
import com.curluin.math.MyVector;

/**
 * Represents a single touched point on the screen with some additional functionality.
 * @author alex
 *
 */
public class TouchPoint {

	private static final String LOG_TAG = "TouchPoint";

	/**
	 * Touch position on screen in percentage of screensize
	 */
	public MyVector screenPosition;
	
	/**
	 * Distance to previous TouchPoint
	 */
	public MyVector offset = MyVector.makeVector2(0, 0);
	
	/**
	 * Instantaneous velocity at that point calculated relative to
	 * another TouchPoint and the duration of the movement between
	 */
	public MyVector velocity;
	
	/**
	 * Point in time when touch occurred in ms
	 */
	long time;
	
	/**
	 * Side of screen on which touch occurred.
	 * -1 left
	 *  0 middle
	 *  1 right
	 */
	int side;

	/**
	 * Constructor
	 * @param position: Touch position on screen relative to screen size
	 * @param time: Point in time when touch occurred
	 */
	public TouchPoint(MotionEvent event, MyVector screenSize) {
		MyVector position = MyVector.makeVector2(event.getX(), event.getY()).divide(screenSize);
		this.screenPosition = position;
		
		// Check touch area
		side = 0;
		if (position.get(0) < 0.4)
			side = -1;
		else if (position.get(0) > 0.6)
			side = 1;
		
		this.time =event.getEventTime();
		velocity = MyVector.makeVector2(0, 0);
	}
	
	/**
	 * Computes and sets instantaneous velocity from previous TouchPoint
	 * @param velocity
	 */
	public void computeInstantaneousVelocity(TouchPoint previousTouchPoint){
		velocity = screenPosition.subtract(previousTouchPoint.screenPosition).divide(time-previousTouchPoint.time);
	}
	
	/**
	 * Computes and sets distance to previous TouchPoint
	 * @param previousTouchPoint
	 */
	public void computeOffset(TouchPoint previousTouchPoint){
		offset = this.screenPosition.subtract(previousTouchPoint.screenPosition);
	}
	
	/**
	 * Prints TouchPoint
	 */
	@Override
	public String toString() {
		return "TP["+ screenPosition + ";"+ side + "]";
	}
	
	public MyVector getOpenGlPosition(Camera camera){
//		float dist=MyVector.makeCopy(camera.getRef()).add(MyVector.makeCopy(camera.getEye()).multiply(-1)).getNorm();
//		float openX=dist*screenPosition.get(0);
//		float openY=0;
//		float openZ=dist*screenPosition.get(1);
		//TODO Make it work for all camera orientations and heights.
		//This works only if the camera looks from above with nearly no rotation. Also Controller.visibleWidth and Controller.visibleLength have to be set right
		float diffTpRX=(screenPosition.get(0)-0.5f)*Controller.visibleWidth;
		float diffTpRY=(screenPosition.get(1)-0.5f)*Controller.visibleLength; 
		MyVector refTrans=MyVector.makeVector3(diffTpRX, 0, diffTpRY);
		MyMatrix rotX=MyMatrix.getRotationMatrix(MyVector.makeVector3(1,0,0), camera.getXRotation());
//		Log.w(LOG_TAG,"Before rot: "+refTrans);
		refTrans=rotX.multiply(refTrans);
//		Log.w(LOG_TAG,"After rot: "+refTrans);
		return camera.getRef().add(refTrans);
	}

	

}
